package com.serkol.ams.module.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.ams.module.sys.entity.SysMenu;
import com.serkol.ams.module.sys.entity.SysUsers;
import com.serkol.ams.module.sys.service.SysMenuService;
import com.serkol.ams.module.sys.mapper.SysMenuMapper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @author Hesen
* @description 针对表【sys_menu(权限表)】的数据库操作Service实现
* @createDate 2024-05-27 19:44:46
*/
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu>
    implements SysMenuService{

    @Cacheable(value = "menu",key = "'findPermissionListByUserId'+#userId")
    @Override
    public List<SysMenu> findPermissionListByUserId(Long userId) {
        return baseMapper.findPermissionListByUserId(userId);
    }

    @Override
    public List<SysMenu> findyTreeList() {
                List<SysMenu> listData=new ArrayList<>();
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNull(SysMenu::getParentId);
        List<SysMenu> list = this.list(queryWrapper);
        if (list != null && list.size() > 0) {
            for (SysMenu sysMenu : list) {
                Long id = sysMenu.getId();
              List<SysMenu> children =findByTreeList(id);
              sysMenu.setChildren(children);
              listData.add(sysMenu);
            }
        }
        return list;
    }

    private List<SysMenu> findByTreeList(Long id) {
        List<SysMenu> list=new ArrayList<>();
          LambdaQueryWrapper<SysMenu> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1.eq(SysMenu::getParentId, id);
                List<SysMenu> list1 = this.list(queryWrapper1);
                if (list1 != null && list1.size() > 0) {

                    for (SysMenu sysMenu : list1) {
                        Long id1 = sysMenu.getId();
                        List<SysMenu> children =findByTreeList(id1);
                        sysMenu.setChildren(children);
                        list.add(sysMenu);
                    }
                }
                return list;
    }
    /**
     * 根据用户id查询权限菜单列表
     */


}




